package com.rf.richfitwheel.workflow.controller;

import com.rf.richfitwheel.admin.annotation.UserOperateLog;
import com.rf.richfitwheel.common.utils.R;
import com.rf.richfitwheel.common.utils.StringUtils;
import com.rf.richfitwheel.workflow.model.WfAuthRule;
import com.rf.richfitwheel.workflow.service.WfAuthRuleService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;


/**
 * 流程目录表
 *
 * @author zhanglu
 * @email zhanglu2@cnpc.com.cn
 * @date 2021-07-20 09:01:22
 */
@RestController
@RequestMapping("/wfauthrule")
public class WfAuthRuleController {

    protected static final Logger log = LoggerFactory.getLogger(WfAuthRuleController.class);

    @Autowired
    private WfAuthRuleService wfAuthRuleService;

    /**
     * 列表
     */
    @RequestMapping("/listByStationProcessOrderId")
    public R list(@RequestBody String id) {
        List<WfAuthRule> authRules = wfAuthRuleService.listByStationProcessOrderId(id);
        return R.ok().put("authRules", authRules);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody String params) {
        if (StringUtils.isNotBlank(params)) {
            JSONObject jsonObject = JSONObject.fromObject(params);
            String folderCode = jsonObject.getString("folderCode");
            String stationProcessOrderId = jsonObject.getString("stationProcessOrderId");
            String authType = jsonObject.getString("authType");
            JSONArray authRules1 = jsonObject.getJSONArray("authRules1");
            JSONArray authRules2 = jsonObject.getJSONArray("authRules2");
            //System.out.println(authRuleVos);
            if (authType.equals("1")) {
                wfAuthRuleService.save(folderCode, stationProcessOrderId, authType, authRules1);
            } else {
                wfAuthRuleService.save(folderCode, stationProcessOrderId, authType, authRules2);
            }
        }
        return R.ok();
    }

    /**
     * 新增修改分级授权
     * @param wfAuthRule
     * @return
     */
    @UserOperateLog(module = "工作流", methods = "saveOneCondition", description = "保存分级授权规则", functionNo = "", logType = "2")
    @RequestMapping("/saveOneCondition")
    public R saveOneCondition(@RequestBody WfAuthRule wfAuthRule) {
        try {
            if (StringUtils.isNotBlank(wfAuthRule.getId())) {
                //修改
                wfAuthRuleService.update(wfAuthRule);
            }else{
                //新增
                wfAuthRuleService.save(wfAuthRule);
            }
            return R.ok();
        } catch (Exception e) {
            log.error("保存授权规则失败", e);
            return R.error("保存授权规则失败");
        }
    }

    @UserOperateLog(module = "工作流", methods = "deleteById", description = "删除分级授权规则", functionNo = "", logType = "2")
    @PostMapping("/deleteById")
    public R deleteById(@RequestBody String id) {
        try {
            wfAuthRuleService.deleteById(id);
            return R.ok();
        } catch (Exception e) {
            log.error("删除授权规则失败", e);
            return R.error("删除授权规则失败");
        }
    }
}
